package cn.wk.quartz.service.impl;

import cn.wk.quartz.domain.QuartzJobInfo;
import cn.wk.quartz.job.Oreder;
import cn.wk.quartz.service.IQuartzService;
import cn.wk.quartz.util.QuartzUtils;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;

@Service
public class QuartzServiceImpl implements IQuartzService {
    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;

    /**
     * 添加定时任务
     * @param quartzJobInfo
     */
    @Override
    public void addJob(QuartzJobInfo quartzJobInfo) {
        Scheduler scheduler = schedulerFactoryBean.getObject();
        QuartzUtils.addJob(scheduler,quartzJobInfo.getJobName(),Oreder.class,
                quartzJobInfo,quartzJobInfo.getCronj());
    }


    /**
     * 移除定时任务
     * @param jobName
     */
    @Override
    public void removeJob(String jobName) {
        Scheduler scheduler = schedulerFactoryBean.getObject();
        QuartzUtils.removeJob(scheduler,jobName);
    }

}
